package com.shandao.shangxiu.util;

import android.text.TextUtils;

import com.ta.utdid2.android.utils.StringUtils;

import java.text.DecimalFormat;

/**
 * 工具类
 * Created by king on 2015/10/28.
 */
public class Utils {


    /**
     * 格式化 银行卡号 后4位
     *
     * @param bankNumber
     * @return
     */
    public static String formatBankNumber(String bankNumber) {
        if (StringUtils.isEmpty(bankNumber)) {
            return "";
        }
        if (bankNumber.length() > 4) {
            return "尾号 (" + bankNumber.substring(bankNumber.length() - 4) + ")";
        }
        return "尾号 (" + bankNumber + ")";
    }

    /**
     * 格式化 昵称 昵称(*春平)
     *
     * @param nickName
     * @return
     */
    public static String formatNick(String nickName, String custName) {
        if (StringUtils.isEmpty(custName)) {
            return nickName;
        }
        if (custName.length() > 1) {
            return nickName + "(*" + custName.substring(1) + ")";

        }
        return nickName + "(*" + custName + ")";
    }

    /**
     * 格式化 身份证
     *
     * @return
     */
    public static String formatCertNo(String certNo) {
        if (StringUtils.isEmpty(certNo)) {
            return "";
        }
        if (certNo.length() > 6) {
            return certNo.substring(0, 6) + "*********";

        }
        return certNo + "*********";
    }

    /**
     * 格式化 姓名 金**
     *
     * @return
     */
    public static String formatName(String name) {
        if (StringUtils.isEmpty(name)) {
            return "";
        }
        if (name.length() > 1) {
            return name.substring(0, 1) + "**";
        }
        return name + "*";
    }

    /**
     * 格式化 有效期2015年12月12日
     *
     * @param validdate
     * @param type      scan扫描以后后格式化  server服务器返回的格式化
     * @return
     */
    public static String formatValiddate(String validdate, String type) {

        if (!TextUtils.isEmpty(validdate) && validdate.length() > 1) {
            StringBuilder sb = new StringBuilder();
            if ("scan".equals(type)) {
                validdate = validdate.substring(validdate.indexOf("-") + 1);
            }
            sb.append(validdate.substring(0, 4));
            sb.append("年");
            sb.append(validdate.substring(4, 6));
            sb.append("月");
            sb.append(validdate.substring(6));
            sb.append("日");
            return sb.toString();
        }
        return validdate;
    }

    /**
     * 格式化 金额小数位50000.00
     *
     * @return
     */
    public static String formatAmountFor2Decimal(String amount) {

        if (StringUtils.isEmpty(amount) || amount.equals("0")
                || amount.equals("0.0") || amount.equals("00")) {
            return "0.00";
        }
        if (!StringUtils.isEmpty(amount) && amount.startsWith("-")) {
            amount = amount.substring(1);
        }
        double mGTotal;
        try {
            mGTotal = Double.parseDouble(amount);
            String result = new DecimalFormat("#####.00").format(mGTotal);
            if (result.startsWith("."))
                result = "0" + result;
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            ToastUtils.show("你输入的金额格式错误");
            return "";
        }
    }

    /**
     * 格式化 手机号码158****0638
     *
     * @return
     */
    public static String formatBindMobile(String mobile) {

        if (StringUtils.isEmpty(mobile) || !PatternCheck.isMobile(mobile)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(mobile.subSequence(0, 3));
        sb.append("****");
        sb.append(mobile.substring(mobile.length() - 4));
        return sb.toString();
    }

    /**
     * 格式化 邮箱158****@163.com
     *
     * @return
     */
    public static String formatEmail(String email) {
        if (StringUtils.isEmpty(email) || !PatternCheck.isEmail(email)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(email.subSequence(0, 3));
        sb.append("****");
        sb.append(email.substring(email.lastIndexOf("@")));
        return sb.toString();
    }


    /**
     * 检查密码复杂性
     *
     * @param passwordStr
     * @return
     */
    public static boolean checkPassword(String passwordStr) {
        String str1 = "^((?![a-zA-Z]+$)(?![0-9]+$))(?![\\*\\_\\,\\!\\?\\:\\@\\-\\#\\^\\[\\]\\{\\}]+$).{8,20}$"; // 由字母、数字组成，不超过20位
        String str2 = ".*[\u2E80-\u9FFF].*";

        if (passwordStr.matches(str2)) {
            return false;
        }

        if (passwordStr.matches(str1)) {
            return true;
        }

        return false;
    }


}
